﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Graphics.PackedVector;



namespace WindowsGame
{
	public interface IHeadUpDisplayLogic
	{
		double AverageFPS { get; }
		double FramePerSeconds { get; }
		double MillisecsPerFrame { get; }
		bool Display { get; }
	}



	public class HeadUpDisplayLogic : IHeadUpDisplayLogic
	{
		public const float AVERAGE_FPS_ALPHA_PARAMETER = 0.1f;
		uint frameCount = 0;
		double secondsElapsed = 0.0;

		double averageFPS = -1.0;
		double framePerSeconds = 0.0;
		double millisecsPerFrame = 0.0;

		bool display = true;

		public double AverageFPS { get { return averageFPS; } }
		public double FramePerSeconds { get { return averageFPS; } }
		public double MillisecsPerFrame { get { return millisecsPerFrame; } }
		public bool Display { get { return display; } }



		public HeadUpDisplayLogic()
		{
		}



		public void ToggleDisplay()
		{
			display = display ? false : true;
		}



		public void Update(GameTime gameTime)
		{
			frameCount++;
			secondsElapsed += gameTime.ElapsedGameTime.TotalSeconds;

			if(secondsElapsed >= 1.0)
			{
				framePerSeconds = frameCount / secondsElapsed;
				millisecsPerFrame = 1000.0 / framePerSeconds;

				if(averageFPS < 0)
					averageFPS = framePerSeconds;
				else
					averageFPS = (1.0 - AVERAGE_FPS_ALPHA_PARAMETER) * averageFPS + AVERAGE_FPS_ALPHA_PARAMETER * framePerSeconds;

				frameCount = 0;
				secondsElapsed = 0.0;
			}
		}
	}
}